package com.glink.manage.controller;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.glink.common.exception.GeneralSimpleException;
import com.glink.common.util.LogUtil;
import com.glink.common.vo.ResponseResult;
import com.glink.manage.dto.org.OrgSaveRequestDTO;
import com.glink.manage.dto.org.OrgUpdateRequestDTO;
import com.glink.manage.service.OrgService;
import com.glink.manage.vo.org.OrgBaseInfoVO;
import com.glink.manage.vo.org.OrgIdRequestDTO;
import com.glink.manage.vo.org.OrgInfoVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Created by IntelliJ IDEA.
 *
 * @Author : qiushaoshan
 * @create 2024/6/18 14:06
 */
@Slf4j
@RestController
@RequestMapping("/API/Manage/orgUnit")
@Api(tags = "组织")
public class OrgController extends AbstractBaseController{

    @Autowired
    private OrgService orgUnitService;

    @ApiOperationSupport(order = 1)
    @ApiOperation(value = "01.1 新增组织", notes = "01.1 新增组织")
    @PostMapping(value = "/insertOrg", produces = MediaType.APPLICATION_JSON_UTF8_VALUE, consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResponseResult<Boolean> insertOrg(@RequestBody @Validated OrgSaveRequestDTO saveRequestDTO) throws GeneralSimpleException {
        log.info("01.1 新增或修改组织：{}", LogUtil.encode(saveRequestDTO));
        boolean status = orgUnitService.insertOrg(saveRequestDTO);
        log.info("01.1 新增或修改组织：success");
        if(!status){
            throw new GeneralSimpleException("402001");
        }
        return this.response(true);
    }

    @ApiOperationSupport(order = 1)
    @ApiOperation(value = "01.2 修改组织", notes = "01.2 修改组织")
    @PostMapping(value = "/updateOrg", produces = MediaType.APPLICATION_JSON_UTF8_VALUE, consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResponseResult<Boolean> updateOrg(@RequestBody @Validated OrgUpdateRequestDTO updateRequestDTO) throws GeneralSimpleException {
        log.info("01.2 修改组织：{}", LogUtil.encode(updateRequestDTO));
        boolean status = orgUnitService.updateOrg(updateRequestDTO);
        log.info("01.2 修改组织：success");
        if(!status){
            throw new GeneralSimpleException("402001");
        }
        return this.response(true);
    }

    @ApiOperationSupport(order = 2)
    @ApiOperation(value = "02.根据ID删除组织", notes = "2.根据ID删除组织")
    @PostMapping(value = "/delOrg", produces = MediaType.APPLICATION_JSON_UTF8_VALUE, consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResponseResult<Boolean> delOrgUnit(@RequestBody @Validated OrgIdRequestDTO requestDTO) throws GeneralSimpleException {
        log.info("02.根据ID删除组织：{}", LogUtil.encode(requestDTO));
        boolean status = orgUnitService.delOrg(requestDTO.getOrgId());
        log.info("02.根据ID删除组织：success");
        return this.response(status);
    }

    @ApiOperationSupport(order = 3)
    @ApiOperation(value = "03.查询组织树", notes = "03.查询组织树")
    @GetMapping(value = "/getOrgTree", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResponseResult<List<OrgInfoVO>> getOrgTree() {
        log.info("03.查询组织树");
        List<OrgInfoVO> orgInfoVO = orgUnitService.getOrgTree();
        log.info("03.查询组织树：success");
        return this.response(orgInfoVO);
    }

    @ApiOperationSupport(order = 4)
    @ApiOperation(value = "04.根据ID查询组织树", notes = "04.根据ID查询组织树")
    @PostMapping(value = "/findById", produces = MediaType.APPLICATION_JSON_UTF8_VALUE, consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResponseResult<OrgBaseInfoVO> findById(@RequestBody @Validated OrgIdRequestDTO dto) {
        log.info("04.根据ID查询组织树,{}", dto);
        OrgBaseInfoVO orgInfoVO = orgUnitService.findById(dto);
        log.info("04.根据ID查询组织树：success");
        return this.response(orgInfoVO);
    }

    @ApiOperationSupport(order = 5)
    @ApiOperation(value = "05.查询组织树-根据登录用户", notes = "05.查询组织树-根据登录用户")
    @ApiImplicitParam(paramType = "header", name = "orgId", value = "当前登录用户的用户id", dataType = "string", required = true)
    @GetMapping(value = "/getOrgTreeByPermission", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResponseResult<List<OrgInfoVO>> getOrgTreeByPermission() throws GeneralSimpleException {
        log.info("05.查询组织树-根据登录用户");
        List<OrgInfoVO> orgInfoVO = orgUnitService.getOrgTreeByPermission();
        log.info("05.查询组织树-根据登录用户：success");
        return this.response(orgInfoVO);
    }
    @ApiOperationSupport(order = 6)
    @ApiOperation(value = "06.查询组织树-参建单位", notes = "06.查询组织树-参建单位")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header", name = "orgId", value = "当前登录用户的orgId", dataType = "string", required = true),
            @ApiImplicitParam(paramType = "header", name = "userId", value = "当前登录用户的userId", dataType = "string", required = true)})
    @GetMapping(value = "/getUnitTree", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResponseResult<List<OrgInfoVO>> getUnitTree() throws GeneralSimpleException {
        log.info("06.查询组织树-参建单位");
        List<OrgInfoVO> orgInfoVO = orgUnitService.getUnitTree();
        log.info("06.查询组织树-参建单位：success");
        return this.response(orgInfoVO);
    }

    @ApiOperationSupport(order = 7)
    @ApiOperation(value = "07.查询组织树-根据登录用户", notes = "07.查询组织树-根据登录用户")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header", name = "orgId", value = "当前登录用户的orgId", dataType = "string", required = true),
            @ApiImplicitParam(paramType = "header", name = "userId", value = "当前登录用户的userId", dataType = "string", required = true)})
    @GetMapping(value = "/getDepartmentOrgTreeByPermission", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResponseResult<List<OrgInfoVO>> getDepartmentOrgTreeByPermission() throws GeneralSimpleException {
        log.info("05.查询组织树-根据登录用户");
        List<OrgInfoVO> orgInfoVO = orgUnitService.getDepartmentOrgTreeByPermission();
        log.info("05.查询组织树-根据登录用户：success");
        return this.response(orgInfoVO);
    }
}
